clear
est clear

cap mat define migrationfigure=J(105,10,.)
		local row=1
		

	use "$input/full_geoid_pair_panel.dta"
	

			
	replace migration=migration/(tot_pop/100000)

	//Make sure only include states that have a storm in at least one county at least once in our sample years
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	//Create future pooled migration - total migrants in the subsequent five years
	sort geoid_pair move_year
	g post_storm=1 if storm==1|storm[_n-1]==1&geoid_pair==geoid_pair[_n-1]|storm[_n-2]==1&geoid_pair==geoid_pair[_n-2]|storm[_n-3]==1&geoid_pair==geoid_pair[_n-3]|storm[_n-4]==1&geoid_pair==geoid_pair[_n-4]|storm[_n-5]==1&geoid_pair==geoid_pair[_n-5]
	mvencode post_storm, mv(0)
	
	preserve
		//Collapse to total migration from a county (not looking at paired county to county migration, just all migration out of a county)
		collapse (sum) migration (mean) share_approved totalapprovedihpamount,by(from_geoid move_year storm storm_fema post_storm state_fips)
		
		
		
		g total_storms=0
		//Create lags of storms (was there a storm in t-1 years)
		sort from_geoid move_year
			foreach i in 1 2 3 4 5{
					g storm_L`i'=storm[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
					replace total_storms=total_storms+storm[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
			}	
				
		label var post_storm "1(0-5 yrs post storm)"
		label var total_storms "All storms in t-5 years"
		label var storm "1(Storm year)"
		foreach var in 1 2 3 4 5{
			label var storm_L`var' "1(Storm year), t-`var'"
		}
		
		//Create leads of storms (was there a storm in t+1 years)
		sort from_geoid move_year
			foreach i in 1 2 3 4 5{
					g storm_E`i'=storm[_n+`i'] if from_geoid==from_geoid[_n+`i'] 
					 
			}	
				
		
		foreach var in 1 2 3 4 5{
			label var storm_E`var' "1(Storm year), t+`var'"
		}
		
		save "$input/temp.dta", replace
	restore
	
	preserve
		//Collapse to total migration from a county (not looking at paired county to county migration, just all migration out of a county)
		collapse (sum) migration (mean) share_approved totalapprovedihpamount,by(from_geoid move_year storm storm_fema post_storm state_fips)
		
		rename storm storm2
		
		g total_storms2=0
		//Create lags of storms (was there a storm in t-1 years)
		sort from_geoid move_year
			foreach i in 1 2 3 4 5{
					g storm2_L`i'=storm2[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
					replace total_storms2=total_storms2+storm2[_n-`i'] if from_geoid==from_geoid[_n-`i'] 
			}	
				
		
		
		
		//Create leads of storms (was there a storm in t+1 years)
		sort from_geoid move_year
			foreach i in 1 2 3 4 5{
					g storm2_E`i'=storm2[_n+`i'] if from_geoid==from_geoid[_n+`i'] 
					 
			}	
				
		
		
		rename from_geoid to_geoid
		save "$input/temp2.dta", replace
	restore
	
	merge m:1 from_geoid move_year using "$input/temp.dta", keep(1 3) nogen
	merge m:1 to_geoid move_year using "$input/temp2.dta", keep(1 3) nogen
	//Create IHS transformations
	ihstrans(migration)
	
	replace totalap=totalap/1000000
	
		
		reghdfe ihs_migration i.storm i.storm2, absorb(i.geoid_pair i.from_geoid  i.move_year i.state_fips) vce(cluster state_fips)
		
			eststo t1c1
			cap mat def migrationfigure[`row',1]=_b[1.storm]
			cap mat def migrationfigure[`row',2]=_se[1.storm]
			cap mat def migrationfigure[`row',3]="no lags, out"
			g sample=e(sample)
			local row=`row'+1
			
				
		reghdfe ihs_migration i.storm i.storm2 i.storm_L* i.storm2_L*, absorb(i.geoid_pair i.from_geoid i.move_year i.state_fips) vce(cluster state_fips)
			eststo t1c2
			
			
			
			lincom 1.storm+1.storm_L1+1.storm_L2+1.storm_L3+1.storm_L4+1.storm_L5
				local beta11=trim("`: display %10.3f r(estimate)'")
				local se11=trim("`: display %10.3f r(se)'")	
				
				local row=`row'+1
				
		
				
		
		
			
	
	clear
	
	use "$input/full_geoid_pair_panel.dta"
	
	bysort state_fips: egen max=max(storm)
		drop if max!=1
		
	sort geoid_pair move_year
	g post_storm=1 if storm==1|storm[_n-1]==1&geoid_pair==geoid_pair[_n-1]|storm[_n-2]==1&geoid_pair==geoid_pair[_n-2]|storm[_n-3]==1&geoid_pair==geoid_pair[_n-3]|storm[_n-4]==1&geoid_pair==geoid_pair[_n-4]|storm[_n-5]==1&geoid_pair==geoid_pair[_n-5]
	mvencode post_storm, mv(0)	
		
	replace migration=migration/(tot_pop/100000)
	
			
	preserve
		collapse (sum) migration ,by(from_geoid to_geoid move_year )

			rename migration in_migration
			rename to_geoid from_geoid2
			rename from_geoid to_geoid
			rename from_geoid2 from_geoid
			save "$input/temp1.dta", replace
	restore
	
		
	
		merge m:1 from_geoid to_geoid move_year using "$input/temp1.dta", keep(1 3) nogen
		merge m:1 from_geoid move_year using "$input/temp.dta",keep(1 3) nogen
		merge m:1 to_geoid move_year using "$input/temp2.dta",keep(1 3) nogen
		
		g net_migration=in_migration-migration
		
		
		ihstrans(net_migration)	
		
		
		
		

		reghdfe ihs_net_migration i.storm i.storm2, absorb(i.from_geoid i.geoid_pair  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t1c4
			cap mat def migrationfigure[`row',1]=_b[1.storm]
			cap mat def migrationfigure[`row',2]=_se[1.storm]
			cap mat def migrationfigure[`row',3]="no lags, net"
			local row=`row'+1
		
				
		reghdfe ihs_net_migration i.storm i.storm_L* i.storm2 i.storm2_L*, absorb(i.from_geoid i.geoid_pair  i.move_year i.state_fips#c.move_year) vce(cluster state_fips)
			eststo t1c5
			
			lincom 1.storm+1.storm_L1+1.storm_L2+1.storm_L3+1.storm_L4+1.storm_L5
				local beta21=trim("`: display %10.3f r(estimate)'")
				local se21=trim("`: display %10.3f r(se)'")	
				
				
		
				
		
			
		
	file open 	t 		using "$tables/table_si13.tex", replace write
	file write t	"\begin{table}[htbp]\footnotesize \centering" _n "\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}" _n ///
						"\caption{Migration impacts of storms with paired counties}\label{tab: total migration paired}" _n  ///
						"\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill}}l*{10}{c}}" _n "\midrule" _n ///
						"&\multicolumn{2}{c}{Impact on out-migration}&\multicolumn{2}{c}{Impact on net migration}\\" _n ///
						"\cmidrule(lr){2-3}\cmidrule(lr){4-5}" _n ///
						"&(1)&(2)&(3)&(4)\\" _n ///
						"\midrule" _n 
	file close 	t
	
				esttab t1c* using "$tables/table_si13.tex", l keep(1.storm 1.storm_L* ) ///
				s(N, l("N") f(%11.0fc) lay(@)) $opts 
				
	file open 	t 		using "$tables/table_si13.tex", append write
	file write t "\\" ///
							"\midrule																			" ///
							"\$\Sigma_{i=0}^{5}Storm_{t-{i}}=1\$			&		&`beta11'&		&`beta21'&				\\" ///
							"												&		&(`se11')&		&(`se21')&				\\"	///
							"\midrule																		" ///
							"Fixed Effects:									&		&		&		&				\\" ///
							"\hspace{3mm}County pair						&Yes	&Yes	&Yes	&Yes			\\ " ///
							"\hspace{3mm}Sending County						&Yes	&Yes	&Yes	&Yes			\\ " ///
							"\hspace{3mm}Year								&Yes	&Yes	&Yes	&Yes			\\ " ///
							"\hspace{3mm}State\$\times\$Year				&Yes	&Yes	&Yes	&Yes			\\ " ///
							"\midrule" _n "\end{tabular*}" _n ///
							"\begin{tabular*}{1\textwidth}{p{6.5in}}" _n ///
							"\footnotesize \textsc{Notes:} All columns report the results of a fixed effects specification with the IHS transformation of the number of out-migrants from a county as the outcome in columns 1-2 and the IHS transformation of net migration as the outcome in columns 3-4. Net migration is defined as in-migration minus out-migration so that negative net migration indicates a declining population. In all regressions we control for storms in the receiving counties. Counties are defined as exposed to a storm if at least one hurricane resulted in a flood warning during the year or the county experienced wind speeds of at least 21 m/s - the speed at which structural damages begins to occur - during a hurricane in the year. 1(Storm year), t-\# indicates that the county experienced a storm \# years previously. \$^{*}\$p=0.1, \$^{**}\$p=0.05,\$^{***}\$p=0.01." ///
							"\end{tabular*}" _n "\end{table}" _n 
	file close 	t		
	
	
	
			
	